package com.lenovo.agricultureclient.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.lenovo.agricultureclient.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
/**
 * 用户引导图片适配器
 * @author zongbingwu
 *
 */
public class StudyImagePagerAdapter extends PagerAdapter 
{
	//图片地址集合
	private ArrayList<String> mDataArray;
	private final LayoutInflater mInflater;
	//异步图片加载属性
	private DisplayImageOptions options;
	//异步图表加载器
	private ImageLoader mImageLoader;
	//图片点击事情
	private View.OnClickListener mClickListener;
	CompoundButton.OnCheckedChangeListener mCheckedChangeListener;

	public StudyImagePagerAdapter(Context context, List<String> array, ImageLoader imageLoader) 
	{
        mDataArray = (ArrayList<String>) array;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mImageLoader = imageLoader;
        //创建默认的异步图片加载属性
        options = new DisplayImageOptions.Builder().build();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public void finishUpdate(View container) {
	}

	@Override
	public int getCount() {
		return mDataArray.size();
	}

	@Override
	public Object instantiateItem(ViewGroup view, int position) 
	{
		//创建图片view容器
		View imageLayout = mInflater.inflate(R.layout.item_pager_image, view, false);
		ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
		CheckBox checkBox = (CheckBox) imageLayout.findViewById(R.id.notShowCheckBox);
		//获取图片路径
		String path = mDataArray.get(position);
		//异步图片加载器根据图片路径加载并显示图片
		mImageLoader.displayImage(path, imageView, options, null);
		imageView.setTag(path);
		//设置图片的点击事件
		imageView.setOnClickListener(mClickListener);
		if(position==2){
			checkBox.setVisibility(View.VISIBLE);
		} else {
			checkBox.setVisibility(View.INVISIBLE);
		}
		checkBox.setOnCheckedChangeListener(mCheckedChangeListener);
		//将图片view加载到容器中
		((ViewPager) view).addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}
	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}
	@Override
	public Parcelable saveState() {
		return null;
	}
	@Override
	public void startUpdate(View container) {
	}
	
	public void setItemClickListener(View.OnClickListener listener){
		mClickListener = listener;
	}
	public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener checkedChangeListener) {
		this.mCheckedChangeListener = checkedChangeListener;
	}
}